package com.hpc.Controller;

import Utils.JSONUtil;
import Utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import entity.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ts/admin")
public class adminController {
    @Resource
    private com.hpc.Service.adminService adminService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private com.hpc.Service.openfeignService openfeignService;
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:18
     * @description: adminLogin超级管理员登录
     * @param:[username, password]
     * @return:java.lang.String
     */
    @PostMapping("/login")
    public String adminLogin(String username,String password){
        int login = adminService.login(username, password);
        if (login!=0){
            return JSONUtil.toJSONString(ResultUtil.success());
        }else {
            return JSONUtil.toJSONString(ResultUtil.error(ResultEnum.ERROR));
        }
    }
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:18
     * @description: userForbidden对用户账号进行封禁
     * @param:[userId]
     * @return:java.lang.String
     */
    @GetMapping("/forbidden")
    public String userForbidden(String userId){
        int i = adminService.userForbidden(userId);
        //将用户token去除重新登录
        Boolean delete = redisTemplate.delete(userId);
        if (i!=0&&delete){
            return JSONUtil.toJSONString(ResultUtil.success());
        }else {
            return JSONUtil.toJSONString(ResultUtil.error(ResultEnum.ERROR));
        }
    }
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:18
     * @description: unForbidden对用户账号解禁
     * @param:[userId]
     * @return:java.lang.String
     */
    @GetMapping("/unforbidden")
    public String unForbidden(String userId){
        int i = adminService.unForbidden(userId);
        if (i!=0){
            return JSONUtil.toJSONString(ResultUtil.success());
        }else {
            return JSONUtil.toJSONString(ResultUtil.error(ResultEnum.ERROR));
        }
    }
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:18
     * @description: deleteUser对用户账号删除
     * @param:[userId]
     * @return:java.lang.String
     */
    @GetMapping("/deleteUser")
    public String deleteUser(String userId){
        int i = adminService.deleteUser(userId);
        //将用户token去除
        if (redisTemplate.hasKey(userId)){
            Boolean delete = redisTemplate.delete(userId);
        }

        if (i != 0){
            List<user> users = adminService.allUserInfo();
            return JSONUtil.toJSONString(ResultUtil.success(users));
        }else {
            return JSONUtil.toJSONString(ResultUtil.error(ResultEnum.ERROR));
        }
    }
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:19
     * @description: allUserInfo获取所有的用户信息进行管理
     * @param:[]
     * @return:java.lang.String
     */
    @GetMapping("/allUserInfo")
    public String allUserInfo(){
        return JSONUtil.toJSONString(ResultUtil.success(adminService.allUserInfo()));
    }
    @GetMapping("/deleteArticle")
    public String deleteArticle(String articleId){
        String s = openfeignService.deleteArticle(articleId);
        CommonResult commonResult = JSON.parseObject(s, CommonResult.class);
        if (commonResult.getRespCode().equals("200")){
            List<article> articles = adminService.allArticleInfo(true);
            return JSONUtil.toJSONString(ResultUtil.success(articles));
        }else {
            return JSONUtil.toJSONString(ResultUtil.error(ResultEnum.ERROR));
        }
    }
    /**
     * @author: Liujd 
     * @createTime: 2023/5/17 18:19
     * @description: allArticleInfo获取所有的文章信息进行管理
     * @param:[]
     * @return:java.lang.String
     */
    @GetMapping("/allArticleInfo")
    public String allArticleInfo(@RequestHeader("userId") String userId,@RequestParam(value = "isNeedAds",required = false) String isNeedAds){
//        System.out.println(userId);
        boolean isNeedAd = isNeedAds != null;
        return JSONUtil.toJSONString(ResultUtil.success(adminService.allArticleInfo(isNeedAd)));
    }
    @GetMapping("/allMessages")
    public String allMessages(){
        return JSONUtil.toJSONString(ResultUtil.success(adminService.getAllMessages()));
    }
    @GetMapping("/deleteMessage")
    public String deleteMessage(String messageId){
        int i = adminService.deleteMessage(messageId);
        if (i!=0){
            Map<String, Object> allMessages = adminService.getAllMessages();
            return JSONUtil.toJSONString(ResultUtil.success(allMessages));
        }else {
            return JSONUtil.toJSONString(ResultUtil.error("删除失败！"));
        }
    }
}
